package utils;

import android.text.TextUtils;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WangLi
 * @version V1.0
 * @date 2015年2月9日 下午5:07:55
 * @Description 解析json工具类
 */

public class JsonUtil {
    /**
     * @param object
     * @return
     * @Description 将对象转为JSON串
     */
    public static String toJson(Object object) {
        Gson gson =  new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.toJson(object);
    }

    /**
     * @param object
     * @return
     * @Description 将对象转为字符串，首字母大写
     */
    //http://blog.csdn.net/lk_blog/article/details/7685190
    public static String toJsonCase(Object object) {
        Gson gson = new GsonBuilder().
                setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)// //会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效.
                .create();
        return gson.toJson(object);
    }

    public static <T> T getObject(String jsonStr, Class<T> cls) {
        Gson gson =  new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.fromJson(jsonStr, cls);
    }
    public static <T> T getObject(JsonElement jsonStr, Class<T> cls) {
        Gson gson =  new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        return gson.fromJson(jsonStr, cls);
    }
    public static <T> List<T> getList(String json, Class<T> cls) {
        List<T> list = new ArrayList<>();
        if (TextUtils.isEmpty(json)) {
            return list;
        }

        try {
            JsonParser parser = new JsonParser();
            JsonArray jsonArray = parser.parse(json).getAsJsonArray();
            Gson gson =  new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

            for (int index = 0; index < jsonArray.size(); index++) {
                JsonElement element = jsonArray.get(index);
                T t = gson.fromJson(element, cls);
                list.add(t);
            }
            return list;


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }


    }

    public static <T> List<T> getList(JsonElement json, Class<T> cls) {
        List<T> list = new ArrayList<>();
        if ( json==null) {
            return list;
        }

        try {
            JsonParser parser = new JsonParser();
            if (json instanceof JsonArray){
                JsonArray jsonArray = (JsonArray) json;
                Gson gson =  new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

                for (int index = 0; index < jsonArray.size(); index++) {
                    JsonElement element = jsonArray.get(index);
                    T t = gson.fromJson(element, cls);
                    list.add(t);
                }
            }


            return list;


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return list;
        }


    }


    public static JsonArray getJsonArrayt(String json) {

        if (TextUtils.isEmpty(json)) {
            return null;
        }
        JsonParser parser = new JsonParser();
        JsonArray jsonArray = parser.parse(json).getAsJsonArray();
        return jsonArray;


    }


}
